﻿using common;
using Model;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;

namespace Dao
{
    /// <summary>
    /// OrderDao数据库操作类
    /// </summary>
    public class OrderDao
    {
        private string sqlStr;

        /// <summary>
        /// 添加新的订单
        /// </summary>
        /// <param name="order">订单</param>
        /// <returns></returns>
        public bool AddNewOrder(Order order)
        {
            sqlStr =
                "insert into orderForm (accountId, bookId, count, orderTime,money,state) values (@accountId, @bookId, @count, getdate(),@money,@state)";
            SqlParameter[] parameters =
            {
                new SqlParameter("@accountId", order.User.Id),
                new SqlParameter("@bookId", order.Book.Id),
                new SqlParameter("@count", order.Count),
                 new SqlParameter("@money", order.Money),
                new SqlParameter("@state", order.State),
            };

            return SqlHelper.ExecNonQuery(sqlStr, parameters) > 0;
        }

        /// <summary>
        /// 通过用户ID获取订单列表
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public List<Order> GetOrdersByAccountId(int id)
        {
            sqlStr =
                "select a.id as id, b.id as bookId,accountId,username,name, image, price, count, orderTime,money,a.state as state from orderForm as a,bookInfo as b, account as c where accountId = @accountId and c.id=accountId and bookId=b.id;";

            SqlParameter[] parameters =
            {
                new SqlParameter("@accountId", id),
            };

            DataTable dataTable = SqlHelper.GetDataTable(sqlStr, parameters);
            List<Order> list = null;
            if (dataTable.Rows.Count > 0)
            {
                list = new List<Order>();
                foreach (DataRow row in dataTable.Rows)
                {
                    Order order = new Order();
                    LoadEntity(order, row);
                    list.Add(order);
                }
            }
            return list;
        }


        /// <summary>
        /// 通过订单状态订单列表
        /// </summary>
        /// <param name="state"></param>
        /// <returns></returns>
        public List<Order> GetOrdersByState(int state)
        {
            sqlStr =
                "select orderForm.id as id, BookInfo.Id as bookId,accountId,username,name,price, image, count, orderTime,money,orderForm.state as state from bookInfo, orderForm,account where orderForm.accountId=account.id and orderForm.bookId=bookInfo.id and OrderForm.state = @state";

            SqlParameter[] parameters =
            {
                new SqlParameter("@state", state),
            };

            DataTable dataTable = SqlHelper.GetDataTable(sqlStr, parameters);
            List<Order> list = null;
            if (dataTable.Rows.Count > 0)
            {
                list = new List<Order>();
                foreach (DataRow row in dataTable.Rows)
                {
                    Order order = new Order();
                    LoadEntity(order, row);
                    list.Add(order);
                }
            }
            return list;
        }


        /// <summary>
        /// 获取所有订单列表信息
        /// </summary>
        /// <returns></returns>
        public List<Order> GetAllOrders()
        {
            sqlStr = "select orderForm.id as id, BookInfo.Id as bookId,accountId,username,name,price, image, count, orderTime,money,orderForm.state as state from bookInfo, orderForm,account where orderForm.accountId=account.id and orderForm.bookId=bookInfo.id";
            DataTable dataTable = SqlHelper.GetDataTable(sqlStr);
            List<Order> list = null;
            if (dataTable.Rows.Count > 0)
            {
                list = new List<Order>();
                foreach (DataRow row in dataTable.Rows)
                {
                    Order order = new Order();
                    LoadEntity(order, row);
                    list.Add(order);
                }
            }
            return list;
        }

        /// <summary>
        /// 根据ID删除一条记录
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public bool DeleteOrderById(int id)
        {
            sqlStr = "delete from orderForm where id = @id";
            SqlParameter[] parameters =
            {
                new SqlParameter("@id", id),
            };

            return SqlHelper.ExecNonQuery(sqlStr, parameters) > 0;
        }

        /// <summary>
        /// 根据ID更新订单状态
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public bool UpdateOrderById(int id,int state)
        {
            sqlStr = "update orderForm set state=@state where id = @id";
            SqlParameter[] parameters =
            {
                new SqlParameter("@id", id),
                new SqlParameter("@state", state),
            };

            return SqlHelper.ExecNonQuery(sqlStr, parameters) > 0;
        }

        /// <summary>
        /// 实体装载
        /// </summary>
        /// <param name="order">装载的对象</param>
        /// <param name="row">数据源</param>
        private void LoadEntity(Order order, DataRow row)
        {
            BookInfo book = new BookInfo();

            book.Id = Convert.ToInt32(row["bookId"]);
            book.Name = row["name"].ToString();
            book.Image ="../../Book_Image/"+row["image"].ToString();
            book.Price = Convert.ToDouble(row["price"]);

            Account user = new Account();
            user.Id = Convert.ToInt32(row["accountId"]);
            user.Username = row["username"].ToString();

            order.Id = Convert.ToInt32(row["id"]);
            order.Book = book;
            order.User = user;
            order.Count = Convert.ToInt32(row["count"]);
            order.OrderTime = Convert.ToDateTime(row["orderTime"]);
            order.Money = Convert.ToDouble(row["money"]);
            order.State = Convert.ToInt32(row["state"]);
        }
    }
}